<?php

/**
 * @author aisyah
 * @copyright 2012
 */
 
include "product.php";

class Product_Dao{
    
    var $product = array();
    var $product_id;
    var $query;
    var $error;
    var $db;    
    
    public function __construct(Database $db){
        $this->db = $db;
        
    }
    public function get_product($id=null, $posisi=null, $batas=null){
        
        $this->db->connect->connect();
        
        if($this->db->table_Exists('product_detail')){
            
            if($id!=null AND $posisi==null AND $batas==null){
                $this->query = "SELECT
                        *
                        FROM
                        product_detail
                        WHERE 
                        productdet_id = '".$id."'
            
                    ";
            }else if($id!=null AND !is_null($posisi) AND !is_null($batas)){
                $this->query = "SELECT
                        *
                        FROM
                        product_detail
                        WHERE 
                        productdet_id = '".$id."'
            
                    LIMIT $posisi,$batas";
                
            }else if(!is_null($posisi) AND !is_null($batas)){
                $this->query = "SELECT
                        *
                        FROM
                        product_detail
                        
                        LIMIT $posisi,$batas";
            }else {
                $this->query = "SELECT
                        *
                        FROM
                        product_detail      
                    ";
            }
            
            
            $result = mysql_query($this->query);  
            
            if($result){
                
                while($r=mysql_fetch_array($result)){
                    
                    $product = new Product();
                    
                    $product->product_id = $r['product_id'];
                    $product->productdet_id = $r['productdet_id'];
                    $product->store_id = $r['store_id'];
                    $product->product_name = $r['product_name'];
                    $product->description = $r['description'];
                    $product->image = $r['image'];
                    $product->price = $r['price'];
                    $product->count_order = $r['count_order'];
                    $product->count_browse = $r['count_browse'];                    
                    
                    $this->product[] = $product;
                    
                }
                
                $this->db->connect->disconnect();
            
                return $this->product;
            }else{
                
                $this->error = 'data tidak ditemukan';
                return $this->error;
                
            }      
            
        }else{
            
            $this->error = 'data tidak ditemukan';
            return $this->error;
            
        }             
        
    } 
    
    
}